/**
 * @Author wywinstonwy
 * @Date 2021/12/28 9:45 下午
 * @Description:
 */
import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';

 class WyDataTable extends StatefulWidget {
   const WyDataTable({Key? key}) : super(key: key);

   @override
   _WyDataTableState createState() => _WyDataTableState();
 }

 class _WyDataTableState extends State<WyDataTable> {
   List<User> data = [
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
     User('老往', 18,'平顶山市区','1990-12-23','1456-67-67'),
     User('老李1', 19,'平顶山市区','1990-12-23','1456-67-67',selected: true),
     User('老张2', 20,'平顶山市区','1990-12-23','1456-67-67'),
     User('老舍3', 21,'平顶山市区','1990-12-23','1456-67-67'),
     User('老牛4', 22,'平顶山市区','1990-12-23','1456-67-67'),
   ];

   var _sortAscending = true;
   @override
   Widget build(BuildContext context) {
     return Scaffold(
       appBar: getAppBar('DataTable'),
       body: _buildDataRows(),
     );
   }
   _baseDataTable(){
     return DataTable(
       sortColumnIndex: 1,
       sortAscending: true,
       columns: [
         DataColumn(label: Text('姓名'),tooltip: '长按提示'),
         DataColumn(label: Text('年龄'),numeric: true,onSort: (int columnIndex,bool ascending){
           //排序算法

         }),
       ],
       rows: [
         DataRow(cells: [
           DataCell(Text('老王')),
           DataCell(Text('26')),
         ]),
         DataRow(
           selected: true,
             onSelectChanged: (selected){
             print(selected);
             },
             cells: [
           DataCell(Text('老李')),
           DataCell(Text('16')),
         ]),
         DataRow(cells: [
           DataCell(Text('老李')),
           DataCell(Text('16')),
         ]),
       ],
     );
   }
   _buildDataRows(){
     List<DataRow> dataRows = [];
     for(int i=0;i<data.length;i++){
       dataRows.add(DataRow(
          selected: data[i].selected,
           onSelectChanged: (selected){
            setState(() {
              data[i].selected = selected!;
            });
           },
           cells: [
             DataCell(Text('${data[i].name}')),
             DataCell(Text('${data[i].age}'),),
             DataCell(Text('${data[i].address}'),),
             DataCell(Text('${data[i].birthday}'),),
             DataCell(Text('${data[i].birthday1}'),),
           ]));
     }
     //外层增加滚动条组件
     return Scrollbar(child: SingleChildScrollView(
       scrollDirection: Axis.vertical,

       child: DataTable(
           sortColumnIndex: 1,
           sortAscending: true,

           columns: [
             DataColumn(label: Text('姓名')),
             DataColumn(label: Text('年龄'),onSort: (int columnIndex,bool ascending){
               setState(() {
                 _sortAscending = ascending;
                 if(ascending){
                   data.sort((a,b)=>a.age.compareTo(b.age));
                 }else{
                   data.sort((a, b) => b.age.compareTo(a.age));
                 }
               });
             }),
             DataColumn(label: Text('地址')),
             DataColumn(label: Text('出身日期')),
             DataColumn(label: Text('出身日期')),

           ],
           rows: dataRows),
     ));
     return ;
   }
 }

class User {
  User(this.name, this.age,this.address,this.birthday,this.birthday1, {this.selected = false});

  String name;
  int age;
  String address;
  String birthday;
  String birthday1;

  bool selected;
}
